<?php
/*
+ ----------------------------------------------------------------------------------------------+
|     e107 website system  : http://e107.org.ru
|     Released under the terms and conditions of the GNU General Public License (http://gnu.org).
|
|     Plugin "my_gallery"
|     Author: Alex ANP alex-anp@ya.ru
+-----------------------------------------------------------------------------------------------+
*/

// The file
$filename = $_GET['img'];
$height = $_GET['h'];
$width= $_GET['w'];

// Get new dimensions
$img_size = getimagesize($filename);
list($width_orig, $height_orig) = $img_size;
$mime = $img_size['mime'];

$support = FALSE;
if ($mime == "image/jpeg") { $image = imagecreatefromjpeg($filename);  $support = TRUE; }
if ($mime == "image/png") { $image = imagecreatefrompng($filename); $support = TRUE; }
if ($mime == "image/gif") { $image = imagecreatefromgif($filename); $support = TRUE; }

if ($support) {

  if ($width>$width_orig) { $width=$width_orig; }
  if ($height>$height_orig) { $height=$height_orig; }
  if (($height_orig/$width_orig)<=($height/$width)) { $height=$width*$height_orig/$width_orig; }
  if (($height_orig/$width_orig)>($height/$width)) { $width=$height*$width_orig/$height_orig; }
  // Resample

  $image_p = imagecreatetruecolor($width, $height);
  //$image = imagecreatefromjpeg($filename);

  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

  // Content type
  header('Content-type: image/jpeg');
  // Output
  imagejpeg($image_p, null, 100);

  imagedestroy($image_p);
  imagedestroy($image);

} else {

  header('Content-type: image/png');
  readfile('images/unsupported.png');

}

?>